/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制
 */
package co.yixiang.controller.buyer.adopt;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import co.yixiang.common.annotation.Log;
import co.yixiang.common.annotation.NoRepeatSubmit;
import co.yixiang.common.constant.ShopConstants;
import co.yixiang.common.enums.ResultCode;
import co.yixiang.common.enums.SortEnum;
import co.yixiang.common.exception.BadRequestException;
import co.yixiang.common.utils.ResultUtil;
import co.yixiang.common.vo.ResultMessage;
import co.yixiang.modules.adopt.entity.domain.YxAdoptProduct;
import co.yixiang.modules.adopt.entity.dto.YxAdoptCategoryDto;
import co.yixiang.modules.adopt.entity.dto.YxAdoptCategoryQueryCriteria;
import co.yixiang.modules.adopt.entity.dto.YxAdoptProductDto;
import co.yixiang.modules.adopt.entity.dto.YxAdoptProductQueryCriteria;
import co.yixiang.modules.adopt.entity.vo.YxAdoptCategoryVo;
import co.yixiang.modules.adopt.entity.vo.YxAdoptProductVo;
import co.yixiang.modules.adopt.service.YxAdoptCategoryService;
import co.yixiang.modules.adopt.service.YxAdoptProductService;
import co.yixiang.modules.goods.entity.domain.YxStoreProduct;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hupeng
 * @date 2019-10-04
 */
@Api(tags = "认养:认养管理")
@RestController
@RequestMapping("/adopt")
public class AdoptBuyerController {

    @Autowired
    private YxAdoptProductService yxAdoptProductService;

    @Autowired
    private YxAdoptCategoryService yxAdoptCategoryService;

    @Log("查询认养分类")
    @ApiOperation(value = "查询认养分类")
    @GetMapping(value = "/category")
    public ResultMessage<List<YxAdoptCategoryVo>> getYxAdoptCategorys() {
        List<YxAdoptCategoryVo> yxAdoptCategory = yxAdoptCategoryService.queryAllByVo();
        return ResultUtil.data(yxAdoptCategory);
    }
    @Log("分页查询认养")
    @ApiOperation(value = "分页查询认养")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cateId", value = "分类ID", paramType = "query"),
            @ApiImplicitParam(name = "sortType", value = "排序类型:SALES、DISTANCE、POPULARITY", paramType = "query"),
            @ApiImplicitParam(name = "adoptName", value = "按名称模糊查询", paramType = "query"),
            @ApiImplicitParam(name = "lng", value = "排序类型为DISTANCE,该字段必须有值", paramType = "query"),
            @ApiImplicitParam(name = "lat", value = "排序类型为DISTANCE,该字段必须有值", paramType = "query")
    })
    @GetMapping
    public ResultMessage<Map<String, Object>> getYxAdoptProducts(YxAdoptProductQueryCriteria criteria, Pageable pageable) {
        criteria.setIsDel(0);
        // 销量 [默认] // 距离排序 // 人气
        Sort sort;
        switch (criteria.getSortType()) {
            case "SALES":
            case "POPULARITY":
                sort = Sort.by(Sort.Direction.DESC, criteria.getSortType());
                break;
            case "DISTANCE":
                sort = Sort.by(Sort.Direction.ASC, criteria.getSortType());
                break;
            default:
                sort = Sort.by(Sort.Direction.DESC, SortEnum.SORT.name());
        }
        Pageable pageableT = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Map<String, Object> stringObjectMap = yxAdoptProductService.queryAllByCateId(criteria, pageableT);
        return ResultUtil.data(stringObjectMap);
    }

    @Log("查询认养详情")
    @ApiOperation(value = "查询认养详情")
    @GetMapping(value = "detail/{id}")
    public ResultMessage<YxAdoptProductVo> getYxStoreProductsDetail(@PathVariable Long id) {
        final YxAdoptProductVo yxAdoptProductVo = yxAdoptProductService.queryAdoptDetail(id);
        return ResultUtil.data(yxAdoptProductVo);
    }

//    @ApiOperation(value = "获取属性")
//    @GetMapping(value = "/attr/{id}")
//    public ResultMessage<JSONObject> attr(@PathVariable Integer id) {
//        String str = yxAdoptProductService.getAdoptProductAttrResult(id);
//        if (StrUtil.isEmpty(str)) {
//            return ResultUtil.success();
//        }
//        JSONObject jsonObject = JSON.parseObject(str);
//        return ResultUtil.data(jsonObject);
//    }


}
